package com.woniu.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.Service.FlowerTypeService;
import com.woniu.entity.FlowerType;
import com.woniu.mapper.FlowerTypeMapper;
import com.woniu.vo.FlowerTypeVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: Gwj
 * @Date: 2024/7/8 14:54
 */
@Service
@Transactional
public class FlowerTypeServiceImpl implements FlowerTypeService {
    @Resource
    private FlowerTypeMapper flowerTypeMapper;

    /**
     * 分页查询 鲜花分类
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "flowerType", key = "#root.methodName+#root.args[0]+#root.args[1]")
    public PageInfo<FlowerTypeVo> findAllFlowerTypes(int pageNum, int pageSize) throws Exception {
        Page<FlowerTypeVo> page = PageHelper.startPage(pageNum, pageSize);
        flowerTypeMapper.findFlowerTypes();
        return page.toPageInfo();
    }

    /**
     * 模糊查询 并且分页
     *
     * @param flowerTypeVo
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<FlowerTypeVo> findFlowerTypesByConditions(FlowerTypeVo flowerTypeVo, int pageNum, int pageSize) throws Exception {
        Page<FlowerTypeVo> page = PageHelper.startPage(pageNum, pageSize);
        flowerTypeMapper.selectFlowerTypesByLikeName(flowerTypeVo);
        return page.toPageInfo();
    }

    /**
     * 删除鲜花分类，逻辑删除
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "flowerType", allEntries = true)
    public int changeFlowerTypeIsDeleted(FlowerType flowerType) throws Exception {
        flowerType.setIsDelete(1);
        return flowerTypeMapper.updateByPrimaryKeySelective(flowerType);
    }

    /**
     * 新增鲜花分类
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "flowerType", allEntries = true)
    public int addFlowerType(FlowerType flowerType) throws Exception {
        flowerType.setCreateTime(new Date());
        flowerType.setIsDelete(0);
        return flowerTypeMapper.insertSelective(flowerType);
    }

    /**
     * 修改鲜花分类信息
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "flowerType", allEntries = true)
    public int updateFlowerTypeInfoById(FlowerType flowerType) throws Exception {
        return flowerTypeMapper.updateByPrimaryKeySelective(flowerType);
    }

    /**
     * 禁用鲜花分类
     *
     * @param flowerType
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "flowerType", allEntries = true)
    public int disableFlowerType(FlowerType flowerType) throws Exception {
        flowerType.setIsDelete(2);
        return flowerTypeMapper.updateByPrimaryKeySelective(flowerType);
    }

    /**
     * 新增时的重名验证
     *
     * @param flowerTypeName
     * @return
     * @throws Exception
     */
    @Override
    public FlowerType checkFlowerTypeName(String typeName) throws Exception {
        Example example = new Example(FlowerType.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("typeName", typeName);
        return flowerTypeMapper.selectOneByExample(example);
    }

    /**
     * 修改时的重名验证
     *
     * @param typeName
     * @param typeId
     * @return
     * @throws Exception
     */
    @Override
    public FlowerType selectFlowerTypeNameAndId(String typeName, Integer typeId) throws Exception {
        return flowerTypeMapper.selectFlowerTypeNameAndId(typeId, typeName);
    }
}
